package com.andy.beef.app.room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.andy.beef.app.App
import com.andy.module.bean.CodeBean

/**
 * @creator:LuoFan
 * @date:2022/6/30
 * @description:
 */
@Database(entities = [CodeBean::class], version = 1, exportSchema = true)
abstract class BeefRoomDatabase : RoomDatabase() {

    //获取codeDao
    abstract fun getCodeDao(): CodeDao

    //获取单例
    companion object {

        @JvmStatic
        @Volatile
        private var INSTANCE: BeefRoomDatabase? = null

        @JvmStatic
        fun getDatabase(context: Context = App.instance): BeefRoomDatabase {
            val tempInstance = INSTANCE
            if (tempInstance != null) {
                return tempInstance
            }
            synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext, BeefRoomDatabase::class.java,
                    "BeefRoomDatabase"
                ).build()
                INSTANCE = instance
                return instance
            }
        }
    }
}